﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace lchealthy
{
    public partial class frmtest : Form
    {
        private int cid = 0;
        private DataSet cds;
        private int mid = 0;        
        public frmtest(int mid)
        {
            cid = mid;
            InitializeComponent();
        }
        public frmtest(int mccid, int mmid)
        {
            cid = mccid;//可以没有
            mid = mmid;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int MThewType, MAge, MFatIn;
            float MWeight, MBMI, MFatAll, MFatUp, MMetaAll, MMeatUp, MThews, MWater, MBone, MXW, MUBelly, MDBelly, Mwaistline, MHip, MRleg, MRARM, MRSlet;
            string MPIC, MReaction;
            MWeight = myfun.getFloat(txtweight.Text);
            MBMI = myfun.getFloat(lblbmi.Text);
            MFatAll = myfun.getFloat(txtfat.Text);
            MFatUp = myfun.getFloat(txtfatup.Text);
            MMetaAll = myfun.getFloat(txtmeta.Text);
            MMeatUp = myfun.getFloat(txtmeatup.Text);
            MThews = myfun.getFloat(txtthews.Text);
            MThewType = myfun.getInt(cbxthewtype.Text);
            MWater = myfun.getFloat(txtwater.Text);
            MAge = myfun.getInt(txtage.Text);
            MFatIn = myfun.getInt(txtfatin.Text);
            MBone = myfun.getFloat(txtbone.Text);
            MXW = myfun.getFloat(txtxw.Text);
            MUBelly = myfun.getFloat(txtub.Text);
            MDBelly = myfun.getFloat(txtdb.Text);
            Mwaistline = myfun.getFloat(txtwaist.Text);
            MHip = myfun.getFloat(txthip.Text);
            MRleg = myfun.getFloat(txtrleg.Text);
            MRARM = myfun.getFloat(txtrarm.Text);
            MRSlet = myfun.getFloat(txtrslet.Text);
            MReaction = txtreaction.Text;
            MPIC = cid + "\\" + DateTime.Now.ToString("yyyyMMdd");
            if (mid == 0)
            {
                string sql = string.Format("INSERT INTO measureInfo ( [CID] ,[MWeight] ,[MBMI] ,[MFatAll] ,[MFatUp] ,[MMetaAll] ,[MMeatUp] ,[MThews] ,[MThewType] ,[MWater] ,[MAge] ,[MFatIn] ,[MBone] ,[MXW] ,[MUBelly] ,[MDBelly] ,[Mwaistline] ,[MHip] ,[MRleg] ,[MRARM] ,[MRSlet] ,[MPIC] ,[MReaction] ) VALUES ({0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20},'{21}','{22}')", cid, MWeight, MBMI, MFatAll, MFatUp, MMetaAll, MMeatUp, MThews, MThewType, MWater, MAge, MFatIn, MBone, MXW, MUBelly, MDBelly, Mwaistline, MHip, MRleg, MRARM, MRSlet, MPIC, MReaction);
                try
                {
                    int i = DbHelperOleDb.ExecuteSql(sql);
                    if (i == 1)
                    {
                        MessageBox.Show("添加测量数量成功！");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("添加测量数量失败！");
                    }
                }
                catch (Exception ex)
                {

                    MessageBox.Show("添加出错！" + ex.Message);
                }
            }
            else
            {
                string sql = string.Format("update measureInfo set [MWeight]={0} ,[MBMI]={1} ,[MFatAll]={2} ,[MFatUp]={3} ,[MMetaAll]={4} ,[MMeatUp]={5} ,[MThews]={6} ,[MThewType]={7} ,[MWater]={8} ,[MAge]={9} ,[MFatIn]={10} ,[MBone]={11} ,[MXW]={12} ,[MUBelly]={13} ,[MDBelly]={14} ,[Mwaistline]={15} ,[MHip]={16} ,[MRleg]={17} ,[MRARM]={18} ,[MRSlet]={19} ,[MReaction]='{20}' where mid={21}", MWeight, MBMI, MFatAll, MFatUp, MMetaAll, MMeatUp, MThews, MThewType, MWater, MAge, MFatIn, MBone, MXW, MUBelly, MDBelly, Mwaistline, MHip, MRleg, MRARM, MRSlet, MReaction,mid);
                try
                {
                    int i = DbHelperOleDb.ExecuteSql(sql);
                    if (i == 1)
                    {
                        MessageBox.Show("修改测量数量成功！");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("修改测量数量失败！");
                    }
                }
                catch (Exception ex)
                {

                    MessageBox.Show("修改出错！" + ex.Message);
                }

            }

        }
        private void txtweight_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {

            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13 && e.KeyChar != 45 && e.KeyChar != 46)
            {
                e.Handled = true;
            }

            //输入为负号时，只能输入一次且只能输入一次
            if (e.KeyChar == 45 && (((TextBox)sender).SelectionStart != 0 || ((TextBox)sender).Text.IndexOf("-") >= 0)) e.Handled = true;
            if (e.KeyChar == 46 && ((TextBox)sender).Text.IndexOf(".") >= 0) e.Handled = true;
            if (e.KeyChar == '\r')
            {
                SendKeys.Send("{TAB}");
            }

        }
        private void txtweight_Leave(object sender, System.EventArgs e)
        {
            if (txtweight.Text.Trim() != "")
                lblbmi.Text = myfun.getBMI(float.Parse(cds.Tables[0].Rows[0][1].ToString()), float.Parse(txtweight.Text)).ToString("F2");
        }
        private void btncancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
        private void frmtest_Load(object sender, EventArgs e)
        {
            string sql = "";
            if (cid != 0)
            {
                sql = "select cname,cheight,cbirthday from customer where cid=" + cid;
                cds = DbHelperOleDb.Query(sql);
                if (cds.Tables[0].Rows.Count == 1)
                {
                    DateTime dt = DateTime.Parse(cds.Tables[0].Rows[0][2].ToString());
                    this.Text = cds.Tables[0].Rows[0][0].ToString();
                    this.Text += (DateTime.Now.Year - dt.Year) + "岁";
                }
            }
            if (mid != 0)
            {
                sql = "select [CID],[MDate],[MWeight],[MBMI],[MFatAll],[MFatUp],[MMetaAll],[MMeatUp],[MThews],[MThewType],[MWater],[MAge],[MFatIn],[MBone],[MXW],[MUBelly],[MDBelly],[Mwaistline],[MHip],[MRleg],[MRARM],[MRSlet],[MReaction] from measureInfo where mid=" +mid;
                DataSet ds = DbHelperOleDb.Query(sql);
                if (ds.Tables[0].Rows.Count == 1)
                {
                    int id = int.Parse(ds.Tables[0].Rows[0][0].ToString());
                    cid = id;
                    sql = "select cname,cheight from customer where cid=" + cid;
                    cds = DbHelperOleDb.Query(sql);
                    lbldate.Text = ds.Tables[0].Rows[0][1].ToString();
                    txtweight.Text = ds.Tables[0].Rows[0][2].ToString();
                    lblbmi.Text = ds.Tables[0].Rows[0][3].ToString();
                    txtfat.Text = ds.Tables[0].Rows[0][4].ToString();
                    txtfatup.Text = ds.Tables[0].Rows[0][5].ToString();
                    txtmeta.Text = ds.Tables[0].Rows[0][6].ToString();
                    txtmeatup.Text = ds.Tables[0].Rows[0][7].ToString();
                    txtthews.Text = ds.Tables[0].Rows[0][8].ToString();
                    cbxthewtype.Text = ds.Tables[0].Rows[0][9].ToString();
                    txtwater.Text = ds.Tables[0].Rows[0][10].ToString();
                    txtage.Text = ds.Tables[0].Rows[0][11].ToString();
                    txtfatin.Text = ds.Tables[0].Rows[0][12].ToString();
                    txtbone.Text = ds.Tables[0].Rows[0][13].ToString();
                    txtxw.Text = ds.Tables[0].Rows[0][14].ToString();
                    txtub.Text = ds.Tables[0].Rows[0][15].ToString();
                    txtdb.Text = ds.Tables[0].Rows[0][16].ToString();
                    txtwaist.Text = ds.Tables[0].Rows[0][17].ToString();
                    txthip.Text = ds.Tables[0].Rows[0][18].ToString();
                    txtrleg.Text = ds.Tables[0].Rows[0][19].ToString();
                    txtrarm.Text = ds.Tables[0].Rows[0][20].ToString();
                    txtrslet.Text = ds.Tables[0].Rows[0][21].ToString();
                    txtreaction.Text = ds.Tables[0].Rows[0][22].ToString();
                   
                }
            }

        }

        private void txtage_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)13 && e.KeyChar != (char)8)
            {
                e.Handled = true;
            }
            if (e.KeyChar == '\r')
            {
                SendKeys.Send("{TAB}");
            }
        }

        private void btnpic_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "图片|*.jpg;*.bmp:*.gif";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {

                string file = openFileDialog1.FileName;
                FileInfo f = new FileInfo(file);
                string path = "";
                if (mid == 0)
                {
                    path = Application.StartupPath + "\\cpics\\" + cid + "\\" + DateTime.Now.ToString("yyyyMMdd");
                }
                else
                {
                    path = Application.StartupPath + "\\cpics\\" + cid + "\\" + (DateTime.Parse(lbldate.Text)).ToString("yyyyMMdd");
                }
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                File.Copy(file, path + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + f.Extension);

            }
        }
    }
}
